﻿#include "ssThirdPersonCamera.h"


namespace StarSeeker
{

ssThirdPersonCamera::ssThirdPersonCamera(void)
{
	m_cameraType = CAMERA_TYPE_TP;
	ComputeEye();
	//SetView();
}

ssThirdPersonCamera::~ssThirdPersonCamera(void)
{
}

void ssThirdPersonCamera::SetViewMatrix()
{
	ssFLOAT3X eye = m_posLookat + m_posEye;
	LookAtRH(m_pmatView, &eye, &m_posLookat, &m_posUp);
	m_transformDirty = false;
	//ComputeProjViewMatrix();
}

void ssThirdPersonCamera::ComputeEye()
{
	//m_posEye.y = sinf(m_fVerAngle)*m_fFocus;
	//m_fHorRadius = cosf(m_fVerAngle)*m_fFocus;
	//m_posEye.x = sinf(m_fHorAngle)*m_fHorRadius;
	//m_posEye.z = cosf(m_fHorAngle)*m_fHorRadius;
	m_posEye.z = m_fVerAngle.Sin()*m_fFocus;
	m_fHorRadius = m_fVerAngle.Cos()*m_fFocus;
	m_posEye.x = m_fHorAngle.Sin()*m_fHorRadius;
	m_posEye.y = m_fHorAngle.Cos()*m_fHorRadius;
	m_Direction = m_posEye;
	m_Direction.Against();
	//m_Direction.Normalize();
}

void ssThirdPersonCamera::SetCoord(const ssFLOAT3X &coord)
{
	m_posLookat = coord;
	//SetView();
	m_transformDirty = true;
}
void ssThirdPersonCamera::SetCoord(ssfloat x, ssfloat y,ssfloat z)
{
	m_posLookat.x = x;
	m_posLookat.y = y;
	m_posLookat.z = z;
	//SetView();
	m_transformDirty = true;
}

void ssThirdPersonCamera::ChangeFocus(ssfloat fVal)
{
	ssfloat fChanged = 0.f;

	if (m_fFocus<50.0f)
	{
		fChanged = m_fFocus+fVal/500.f;
	}
	else if (m_fFocus<100.0f)
	{
		fChanged = m_fFocus+fVal/100.f;
	}
	else if (m_fFocus<1000.f)
	{
		fChanged = m_fFocus+fVal/10.f;
	}
	else
	{
		fChanged = m_fFocus+fVal;
	}
	if (fChanged<0.f) return;
	m_fFocus = fChanged;
	//计算摄像机所在坐标
	ComputeEye();
	//SetView();
	m_transformDirty = true;
}

void ssThirdPersonCamera::ResetDirection()
{
	m_fHorAngle = Math3D::gc_PI;
	m_fVerAngle = Math3D::gc_QUARTER_PI;
	m_fFocus = 50.0f;
	ComputeEye();
	//SetView();
	m_transformDirty = true;
}

void ssThirdPersonCamera::RotatingEye(int x, int y)
{
	if (x)
	{
		ssfloat fx = (ssfloat)x/500.0f;
		ChangeHorizontalAngle(fx);
	}
	if (y)
	{
		ssfloat fy = (ssfloat)y/500.0f;
		ChangeVerticalAngle(fy);
	}
	ComputeEye();
	//SetView();
	m_transformDirty = true;
}

void ssThirdPersonCamera::Walk(bool bGoAhead)
{
	m_posLookat += m_Direction*(bGoAhead?10.f:-10.f);
	//SetView();
	m_transformDirty = true;
}

}
